package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.valve;

import com.alibaba.fastjson.JSON;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.BackMessage;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.ValveMapper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Valve;
import com.cqhilink.iot.jobstandardmanagement.facade.model.Ztree;
import com.cqhilink.iot.jobstandardmanagement.facade.service.organization.JsmOrganizationService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.valve.JsmValveService;
import com.cqhilink.iot.jobstandardmanagement.services.innerservice.util.OrgLevelUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author 卢曼成
 * @Description 阀室、阀井接口实现类
 * @CreateDateTime 2017-05-23 15:02
 **/
public class JsmValveServiceImpl implements JsmValveService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ValveMapper valveMapper;
    @Autowired
    private JsmOrganizationService jsmOrganizationService;

    /**
     * 根据ID查询
     *
     * @param valveId
     * @return
     */
    @Override
    public Valve selectByPrimaryKey(String valveId) {
        return valveMapper.selectByPrimaryKey(valveId);
    }

    /**
     * @param valveId
     * @return Map<String, Object>
     * @Author 卢曼成
     * @Description 逻辑删除
     * @CreateDateTime 2017/5/23 15:01
     */
    @Override
    public Map<String, Object> deleteLists(String valveId) {
        if (StringUtils.isEmpty(valveId)) {
            throw new RuntimeException("参数出错");
        }
        List<String> list = JSON.parseArray(valveId, String.class);
        Map<String, Object> rowMap = Maps.newHashMap();
        try {
            int rowCount = valveMapper.deleteListKey(list);
            if (rowCount > 0) {
                logger.debug("删除阀室阀井:{}成功", list);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
                rowMap.put("msg", BackMessage.DEL_OK);
            } else {
                logger.debug("删除阀室阀井:{}失败", list);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
                rowMap.put("msg", BackMessage.DEL_FAIL);
            }
        } catch (Exception e) {
            logger.error("删除阀室阀井{}失败", list);
            throw new RuntimeException("删除阀室阀井");
        }
        return rowMap;
    }

    /**
     * @param record
     * @return Map<String, Object>
     * @Author 卢曼成
     * @Description 分页查询
     * @CreateDateTime 2017/5/23 15:01
     */
    @Override
    public Map<String, Object> getPageList(Valve record) {
        Map<String, Object> resultMap = Maps.newHashMap();
        if (StringUtils.isEmpty(record.getOrgId())) {
            return resultMap;
        }
        List<Valve> list = valveMapper.queryList(record);
        int count = valveMapper.queryCount(record);
        resultMap.put("rows", list == null ? Lists.newArrayList() : list);
        resultMap.put("total", count);
        return resultMap;
    }

    /**
     * @param record
     * @return Map<String, Object>
     * @Author 卢曼成
     * @Description 保存
     * @CreateDateTime 2017/5/23 15:01
     */
    @Override
    public Map<String, Object> save(Valve record) {
        if (record == null) {
            throw new RuntimeException("参数出错");
        }
        Map<String, Object> rowMap = Maps.newHashMap();
        Date date = new Date();
        record.setValveId(UniqueUtil.uuid());
        record.setCreateDate(date);
        record.setUpdateDate(date);
        record.setBsflag("0");
        try {
            int rowCount = valveMapper.insert(record);
            if (rowCount > 0) {
                logger.debug("保存阀室阀井:{}成功", record);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
                rowMap.put("msg", BackMessage.ADD_OK);
            } else {
                logger.debug("保存阀室阀井:{}失败", record);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
                rowMap.put("msg", BackMessage.ADD_FAIL);
            }
        } catch (Exception e) {
            logger.error("保存阀室阀井{}失败", record);
            throw new RuntimeException("保存阀室阀井");
        }
        return rowMap;
    }

    /**
     * @param record
     * @return Map<String, Object>
     * @Author 卢曼成
     * @Description 修改
     * @CreateDateTime 2017/5/23 15:01
     */
    @Override
    public Map<String, Object> update(Valve record) {
        if (record == null) {
            throw new IllegalArgumentException("参数出错");
        }
        Map<String, Object> rowMap = Maps.newHashMap();
        record.setUpdateDate(new Date());
        try {
            int rowCount = valveMapper.updateByPrimaryKeySelective(record);
            if (rowCount > 0) {
                logger.debug("修改阀室阀井:{}成功", record);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
                rowMap.put("msg", BackMessage.MODIFY_OK);
            } else {
                logger.debug("修改阀室阀井:{}失败", record);
                rowMap.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
                rowMap.put("msg", BackMessage.MODIFY_FAIL);
            }
        } catch (Exception e) {
            logger.error("修改阀室阀井{}失败", record);
            throw new RuntimeException("修改阀室阀井");
        }
        return rowMap;
    }

    /**
     * @param orgId
     * @param pOrgId
     * @param keyword
     * @return List<Ztree>
     * @Author 卢曼成
     * @Description 查询树形
     * @CreateDateTime 2017/5/23 15:01
     */
    @Override
    public List<Ztree> initTree(String orgId, String pOrgId, String keyword) {
        return jsmOrganizationService.initTree(orgId, pOrgId, keyword, OrgLevelUtils.ORG_LEVEL_STATION);
    }


}
